/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bea.cliente.core;

import bea.cliente.gui.builders.LoginScreenBuilder;
import bea.cliente.core.Game;
import bea.cliente.core.GameSettings;
import bea.cliente.core.Memory;
import bea.cliente.gui.builders.DebugConsoleBuilder;
import bea.cliente.gui.builders.MainScreenBuilder;
import bea.cliente.gui.styles.Styles;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import java.util.HashMap;

/**
 *
 * @author Ivan
 */
public class GuiManagerState extends AbstractAppState {
    /**
     * Caminho dos arquivos de controle e estilo
     */
    protected static final String RESOURCE_STYLES = "Interface/styles/nifty-default-styles.xml";
    protected static final String RESOURCE_CONTROLS = "Interface/controls/nifty-default-controls.xml";
    /**
     * Constates com nome das screen/startscreens
     */
    public static final String LOGIN_SCR = "loginScr";
    public static final String MAIN_SCR = "mainScr";
    /*
     * Hash map com as telas do jogo, para que possam 
     * ser carregadas a qualquer momento.
     * O primeiro parametro(key) é tambem o nome da 
     * start screen dentro da tela
     */
    private Game game;
    private GameSettings gameSettings;
    private HashMap<String, Screen> screens;
    // arquivos de interface XML
    // screens
    private NiftyJmeDisplay niftyDisplay;
    private Nifty nifty;
    // console de debug
    private Element debugConsole;

    // screen lists
    public GuiManagerState(Game _game) {
        // Referencia dos objetos do Game
        game = _game;
        gameSettings = game.gameSettings;
    }
    
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        // scene processor para a interface
        niftyDisplay = new NiftyJmeDisplay(game.getAssetManager(), game.getInputManager(), game.getAudioRenderer(), game.getGuiViewPort());
        // resgata o objeto nifty do processo de cena
        nifty = niftyDisplay.getNifty();
        // adiciona o processo na viewport da interface
        game.getGuiViewPort().addProcessor(niftyDisplay);
        // carrega os xml de styles e controles defaults
        nifty.loadStyleFile(RESOURCE_STYLES);
        nifty.loadControlFile(RESOURCE_CONTROLS);
        // inicializa os stilos definidos na programação
        Styles.init(nifty);
        // inicializa as telas
        initScreens();
        // inicia a main screen
        nifty.gotoScreen(MAIN_SCR);
        
        // call parent at the end
        super.initialize(stateManager, app);
    }

    private void initScreens() {
            screens = new HashMap<String, Screen>();
            // constroi a tela de login
            Screen loginScreen = LoginScreenBuilder.build(nifty);
            // constroi a tela principal do jogador
            Screen mainScreen = MainScreenBuilder.build(nifty);
            // guarda as telas no hashmap
            screens.put(LOGIN_SCR, loginScreen);
            screens.put(MAIN_SCR, mainScreen);
            // adiciona as telas a nifty object
            nifty.addScreen(LOGIN_SCR, loginScreen);
            nifty.addScreen(MAIN_SCR, mainScreen);
            //nifty.setDebugOptionPanelColors(true);
            //initDebugConsole();
    }

    /**
     * Constroi e adiciona a tela atual um console de debug
     */
    private void initDebugConsole() {
        Element consoleLayer = new LayerBuilder("debugConsoleLayer") {

            {
                childLayoutCenter();
            }
        }.build(nifty, screens.get(MAIN_SCR), screens.get(MAIN_SCR).getRootElement());
        debugConsole = DebugConsoleBuilder.build(nifty, screens.get(MAIN_SCR), consoleLayer);
    }
}